package _220331;

import java.util.*;

/**
 * @author ShadowLim
 * @create 2022-03-31-18:41
 */

/**
 * 卡片换位：输出0
 */
public class Main {
    static int[] dirs = {1, -1, 3, -3};     // 右 左 下 上
    static Set<String> vis = new HashSet<>();

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] s = new String[2];
        for (int i = 0; i < s.length; i++) {
            s[i] = scanner.nextLine();
        }
        int startA = 0, startB = 0, spacePos = 0;
        Point start = new Point();
        start.str = s[0] + s[1];
        start.step = 0;
        for (int i = 0; i < start.str.length(); i++) {
            if (start.str.charAt(i) == ' ')   spacePos = i;
            if (start.str.charAt(i) == 'A')   startA = i;
            if (start.str.charAt(i) == 'B')   startB = i;
        }
//        System.out.println(spacePos + " " + startA + " " + startB);
        Queue<Point> q = new LinkedList<>();
        q.offer(start);
        vis.add(start.str);
        int ans = 0;
        while (!q.isEmpty()) {
            Point cur = q.poll();
            String tempStr = cur.str;
            int bushu = cur.step;
            ans = bushu;
            if (tempStr.charAt(startA) == 'B' && tempStr.charAt(startB) == 'A') {
                System.out.println(bushu);
                break;
            }
            // 寻找空格
            for (int i = 0; i < tempStr.length(); i++) {
                if (tempStr.charAt(i) == ' ')   spacePos = i;
            }
            for (int i = 0; i < 4; i++) {
                int nSpacePos = spacePos + dirs[i];
                // 越界
                if (nSpacePos < 0 || nSpacePos > tempStr.length() - 1) {
                    continue;
                }
                // 第一行与第二行的末尾处
                if (spacePos == 2 && nSpacePos == 3) {
                    continue;
                }
                if (nSpacePos == 2 & spacePos == 3) {
                    continue;
                }
                String nTempStr = tempStr;
                char s1 = nTempStr.charAt(spacePos);
                char s2 = nTempStr.charAt(nSpacePos);
                s1 = s2;
                s2 = ' ';
                if (!vis.contains(nTempStr)) {
                    Point np = new Point();
                    np.str = nTempStr;
                    np.step = bushu + 1;
                    q.offer(np);
                    vis.add(nTempStr);
                }
            }
        }
        System.out.println(ans);
    }

    static class Point {
        String str;
        int step;

        public Point(){}

        public Point(String str, int step) {
            this.str = str;
            this.step = step;
        }
    }
}
